/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package cn.edu.just.secondwebapp.daoimpl;

import cn.edu.just.secondwebapp.dao.PersonDao;
import cn.edu.just.secondwebapp.entity.Person;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author JUST
 */
public class PersonDaoImpl implements PersonDao {

    Connection connection;

    public PersonDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public List<Person> findAll(String fuzzyKey) throws SQLException {
        List<Person> result = new ArrayList<>();

        String sql = "select * from person where name like ?";
        PreparedStatement st = connection.prepareStatement(sql);
        st.setString(1, "%" + fuzzyKey + "%");
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            int pid = rs.getInt(1);
            String name = rs.getString(2);
            int age = rs.getInt(3);
            Date birthday = rs.getDate(4);
            BigDecimal gongzi = rs.getBigDecimal(5);
            Person p = new Person(pid, name, age, birthday, gongzi);
            result.add(p);
        }
        rs.close();
        st.close();
        return result;
    }

    @Override
    public boolean doCreate(Person p) {
        boolean result = false;
        PreparedStatement pst = null;
        try {            
            String sql = "insert into person(name,age,birthday,salary) values(?,?,?,?)";
            pst = connection.prepareStatement(sql);
            pst.setString(1, p.getName());
            pst.setInt(2, p.getAge());
            pst.setDate(3,new java.sql.Date(p.getBirthday().getTime()));
            pst.setBigDecimal(4, p.getSalary());
            int rows = pst.executeUpdate();
            if (rows > 0) {
                result= true;
            } 
           
        } catch (SQLException ex) {            
            ex.printStackTrace();
        }finally{
            try {
                pst.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
         return result;
    }

    @Override
     public boolean doUpdate(Person p) {
        boolean result = false;
        try {            
            String sql = "update person set name=?,age=?,birthday=?,salary=? where pid = ?";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, p.getName());
            pst.setInt(2, p.getAge());
            pst.setDate(3,new java.sql.Date(p.getBirthday().getTime()));
            pst.setBigDecimal(4, p.getSalary());
            pst.setInt(5, p.getPid());
            int rows = pst.executeUpdate();
            if (rows > 0) {
                result= true;
            }            
        } catch (SQLException ex) {            
            ex.printStackTrace();
        }
         return result;
    }
     
    @Override
     public boolean doRemove  (int id) {
        boolean result = false;
        try {            
            String sql = "delete from person where pid = ?";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setInt(1, id);           
            int rows = pst.executeUpdate();
            if (rows > 0) {
                result= true;
            }            
        } catch (SQLException ex) {            
            ex.printStackTrace();
        }
         return result;
    }
    
}
